<?php

namespace App\Http\Controllers\V1\Excel;

use App\Events\V1\ImportExportEvent;
use App\Http\Requests\V1\ImportDataRequest;
use App\Models\V1\BasicCollectors;
use App\Models\V1\BasicCollectTransformers;
use App\Models\V1\BasicCustomers;
use App\Models\V1\BasicTransformers;
use Illuminate\Http\Request;
use Jenssegers\Agent\Agent;
use Illuminate\Support\Facades\Auth;
use Maatwebsite\Excel\Facades\Excel;
use Log;


class CollectDataExcelController extends BaseExcelUploadController
{
    /**
     * 显示客户采集数据管理页面
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('v1.customers_report');
    }

    /**
     * 导出采集数据文件
     *
     * @param Request $request
     */
    public function exportData(Request $request,$customer_code)
    {
        $clientIP = $request->getClientIp();

        // 客户
        $customer = BasicCustomers::where('code',$customer_code)->first();
        // 获取客户对应的变压器
        $transformers = BasicTransformers::where('customer_code',$customer_code)->get();
        $collectorCodes = array();
        // 变压器编号与容量
        $str = '';
        foreach ($transformers as $transformer){
            $str = $str. $transformer['customer_transformer_code'];
            // 获取变压器对应的采集器
            $collect_code = BasicCollectors::where('transformer_code',$transformer['code'])->value('code');
            array_push($collectorCodes,$collect_code);
        }
        // 获取对应采集器采集的数据
        $custodian = array();
        foreach ($collectorCodes as $collectorCode){
            $collectDatas = BasicCollectTransformers::where('collector_code',$collectorCode)->get();
            foreach ($collectDatas as $collectData){
                array_push($custodian,$collectData);
            }
        }
        $header1 = ['用户名称：'.$customer['name'],'' ,'','变压器编号与容量：'.$str,''];
        $header2 = ['名称','温度','电压','电流','记录时间'];

//        all(['name','temperature','voltage','electric','created_at']);
        $cellDatas = [$header1,$header2];
        foreach ($custodian as $item) {
            $cellData = [
                $item->name,
                $item->temperature,
                $item->voltage,
                $item->electric,
                $item->created_at,
            ];
            array_push($cellDatas, $cellData);

        }

        Excel::create(iconv('UTF-8', 'GBK','变压器运行记录'), function ($excel) use ($cellDatas, $clientIP) {
            $excel->sheet('变压器运行记录', function ($sheet) use ($cellDatas) {
                $sheet->rows($cellDatas);

                $sheet->mergeCells('A1:C1');
                $sheet->mergeCells('D1:E1');

                /** 此为针对每行的高宽进行设置 */
                for ($i = 1; $i <= count($cellDatas); $i++) {
                    $sheet->setHeight($i, 30);
                    if ($i > 1){
                        $sheet->row($i, function ($row) {
                            /** @var CellWriter $row */
                            $row->setAlignment('center');
                            $row->setValignment('center');
                        });
                    }

                }

//                $sheet->cells('A2:E2', function($cells) {
//                    $cells->setAlignment('center');
//                    $cells->setValignment('center');
//                });
                $sheet->cells('A1:E1', function($cells) {
                    $cells->setBackground('#e1e2e4');
                    $cells->setFont(array(
                        'size' => '10',
                        'bold' =>  true
                    ));
                });
//                $sheet->cells('A2:E2', function($cells) {
//                    $cells->setAlignment('center');
//                    $cells->setBackground('#a6a5a5');
//                    $cells->setFont(array(
//                        'size' => '10',
//                        'bold' =>  true
//                    ));
//                });
                $sheet->setWidth(array(
                    'A'     =>  15,
                    'B'     =>  15,
                    'C'     =>  15,
                    'D'     =>  15,
                    'E'     =>  20,
                ));
            });

            $response['rspMsg'] = [
                'type' => 'export',
                'msg' => '成功下载[采集数据]导出文件',
            ];
            event(new ImportExportEvent(Auth::user(), new Agent(), $clientIP, time(), $response['rspMsg']));
        })->export('xls');
    }
}
